// 多源BFS
// 长草
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int N = 1001;
char arr[N][N];
ll n, m, k;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
queue<pair<int, int>> q;

int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            char ch;
            cin >> ch;
            if (ch == 'g') q.push({i, j});
            arr[i][j] = ch;
        }
    }
    cin >> k;
    while (q.size() && k--)
    {
        int sz = q.size();
        while (sz--)
        {
            auto [a, b] = q.front();
            q.pop();
            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 1 && x <= n && y >= 1 && y <= m && arr[x][y] == '.')
                {
                    arr[x][y] = 'g';
                    q.push({x, y});
                }
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cout << arr[i][j];
        }
        cout << endl;
    }
    return 0;
}

// 回文字符串
#include <bits/stdc++.h>
using namespace std;

bool check(const string& str)
{
    int i = 0, j = str.size() - 1;
    while (i < j)
    {
        if (str[i] == str[j]) i++, j--;
        else if (str[j] == 'l' || str[j] == 'q' || str[j] == 'b') j--;
        else if (str[i] == 'l' || str[i] == 'q' || str[i] == 'b') i++;
        else return false;
    }
    return true;
}
int main()
{
    int t; cin >> t;
    while (t--)
    {
        string str; cin >> str;
        cout << (check(str) == true ? "Yes" : "No") << endl;
    }
    return 0;
}

// 螺旋矩阵(模拟)
class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
        int m = matrix.size(), n = matrix[0].size();
        vector<int> ret(m * n);
        vector<vector<bool>> used(m, vector<bool>(n));
        int a = 0, b = 0, d = 1; // 开始的方向是向右
        for (int i = 0; i < m * n; i++)
        {
            ret[i] = matrix[a][b];
            used[a][b] = true;
            int x = a + dx[d], y = b + dy[d];
            if (x < 0 || x == m || y < 0 || y == n || used[x][y])
            {
                d = (d + 1) % 4;
                x = a + dx[d], y = b + dy[d];
            }
            a = x, b = y;
        }
        return ret;
    }
};
    